package DBLayer;

import static org.junit.Assert.*;

import org.junit.Test;

import ModelLayer.*;
import java.sql.SQLException;

/**
 * @author Gruppe 2
 * 
 */
public class DBOrderTest {

	@Test
	public void testSelectSingleOrder() {
		IFDBOrder dbo = new DBOrder();
		try {
			SalesOrder s = dbo.selectSingleOrder(1);
			System.out.println("Customer ID: " + s.getCustomer().getId());
			System.out.println("Number of salelines: "
					+ s.getSalesLineArray().size());
		} catch (SQLException e) {
			e.printStackTrace();
			fail("Unable to get datafrom DB");
		}
	}

	@Test
	public void testSelectSingleOrderNotFound() {
		IFDBOrder dbo = new DBOrder();
		try {
			SalesOrder s = dbo.selectSingleOrder(999);
			if (s != null) {
				System.out.println("Customer ID: " + s.getCustomer().getId());
				System.out.println("Number of salelines: "
						+ s.getSalesLineArray().size());
			} else {
				System.out.println("Order Not Found");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			fail("Unable to get datafrom DB");
		} catch (Exception e) {
			e.printStackTrace();

		}
	}

	@Test
	public void testUpdateSalesOrder() {
		IFDBOrder dbo = new DBOrder();
		try {
			SalesOrder s = dbo.selectSingleOrder(1);

			System.out.println("Sales Order: " + s.getOrderNo());
			System.out.println("Customer ID: " + s.getCustomer().getId());
			System.out.println("Number of salelines: "
					+ s.getSalesLineArray().size());

			dbo.updateSalesOrder(s);
		} catch (SQLException e) {
			e.printStackTrace();
			fail("Update not working");
		}
	}

	@Test
	public void testInsertSalesOrder() {
		IFDBOrder dbo = new DBOrder();
		IFDBProduct dbp = new DBProduct();
		SalesOrder order = new SalesOrder(0, "10/10/10", "Ready", "10/10/10",
				0, new Customer());
		order.getCustomer().setId(1);
		try {
			order.addSalesLine(new SalesLine(0, order, dbp
					.selectSingleProduct(1), 1));
			order.addSalesLine(new SalesLine(0, order, dbp
					.selectSingleProduct(2), 3));
			order.addSalesLine(new SalesLine(0, order, dbp
					.selectSingleProduct(3), 2));
			dbo.insertSalesOrder(order);
		} catch (SQLException e) {
			e.printStackTrace();
			fail("Not yet implemented");
		}
	}
}
